package cn.edu.sig.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Goods;
import cn.edu.sig.bean.Message;
import cn.edu.sig.bean.Style;
import cn.edu.sig.bean.User;
import cn.edu.sig.service.GoodsService;
import cn.edu.sig.service.MessageService;
import cn.edu.sig.service.StyleService;
@Controller
public class MessageController {
	
	@Resource(name="goodsService")
	private GoodsService goodsService;
	
	@Resource(name="messageService")
	private MessageService messageService;
	
	@Resource(name = "styleService")
	private StyleService styleService;
	
	@RequestMapping(value="addMessage.html", method = RequestMethod.GET)
	public String addMessagePage(
			@ModelAttribute(value="code")User user,
			@RequestParam(value="status")int status,
			Model model){
		
		Goods goods = new Goods();
		
		goods.setUser(user);
		
		List<Goods> goodsList = goodsService.findByStatu(user, status);
		
		model.addAttribute("goodsList", goodsList);
		model.addAttribute("user", user);
		
		return "common/addMessage";
		
	}
	
	@RequestMapping(value="addMessage.action", method = RequestMethod.POST)
	public String addMessage(
			@ModelAttribute(value="message")Message message,
			Model model){
		
		Date createTime = new Date();
		
		message.setCreateTime(createTime);
		message.getGoods().setPublicTime(createTime);
		goodsService.updateGoods(message.getGoods());
		messageService.addMessage(message);
		
		return "redirect:goodsList.html?code="+message.getUser().getCode();
		
	}
	
	@RequestMapping(value="messageList.html", method = RequestMethod.GET)
	public String findAllMessage(
			@RequestParam(value="pageNumber" ,required = true)Integer pageNo,
			@ModelAttribute(value="user")User user,
			Model model){
		
		CurrentPage<Message> list = messageService.findAllMessage(pageNo, 5);
		List<Style> styles = styleService.findAll(0);
		model.addAttribute("styles", styles);
		model.addAttribute("list", list);
		model.addAttribute("user", user);
		return "messageList";
	}
}
